﻿using System;
using System.Web.Mvc;
using Data;
using Models;

namespace Web.Controllers
{
    public class CategoryController : BaseController
    {
        public CategoryController()
        {
            var dbCategory = new CategoryData();
            ViewBag.NewsCategories = dbCategory.GetCategories();
            var dbTags = new TagsData();
            ViewBag.NewsTags = dbTags.GetTags();
        }

        public ActionResult Index(int? page, string id)
        {
            if (id != null)
            {
                var dbNews = new NewsData();
                var dbCategory = new CategoryData();
                var categoryList = dbCategory.GetCategories(name:id);
                if (categoryList != null)
                {
                    var category = categoryList[0];

                    var newsList = dbNews.GetNews(category:category.Id);

                    if (newsList != null)
                    {
                        Pager pager = new Pager(newsList.Count, Request.Url.LocalPath, page);
                        ViewBag.pager = pager;

                        if (pager.Check() == null)
                        {
                            Redirect(Request.Url.LocalPath);
                        }
                        return View(newsList);

                    }
                }

                ViewBag.Message = "Empty category";
                return View();
            }
            return Redirect("/home");
        }

    }
}
